﻿//Sony Computer Entertainment Confidential

using System;
using System.Windows.Forms;
using System.Collections.Generic;

using Sce.Atf;
using Sce.Atf.Applications;

namespace Sce.Atf.Dom
{
    /// <summary>
    /// Toolstrip GUI for specifying a search on DomNodes</summary>
    public class DomNodeSearchToolStrip : SearchToolStrip
    {
		/// <summary>
        /// Constructor</summary>
		public DomNodeSearchToolStrip() : base()
        {
            // Define query tree, which defines the layout of the search toolstrip GUI
            m_rootNode = new DomNodeQueryRoot();
            QueryTree.AddLabel(m_rootNode, "Find node(s)");

            // Add option to search DomNodes on either their name, or parameters
            QueryOption searchNameOrParam = QueryTree.AddOption(m_rootNode);
            QueryTreeDomNode.AddDomNodeNameQuery(QueryTree.AddOptionItem(searchNameOrParam, "whose name", 0), true);
            QueryTreeDomNode.AddDomNodePropertyQuery(QueryTree.AddOptionItem(searchNameOrParam, "with a parameter", 0));
            QueryTree.AddSeparator(m_rootNode);
            m_rootNode.RegisterSearchButtonPress(QueryTree.AddButton(m_rootNode, "Search"));

            // Entering text into the toolstrip will trigger a search, changing an option rebuilds the toolstrip GUI
            m_rootNode.SearchTextEntered += searchSubStrip_SearchTextEntered;
            m_rootNode.OptionChanged += searchSubStrip_OptionsChanged;

            // 
            // Build toolStrip GUI by retrieving toolstrip item list from tree, and adding 
            // them to ToolStrip.Items
            // 
            SuspendLayout();
            List<ToolStripItem> toolStripItems = new List<ToolStripItem>();
            m_rootNode.GetToolStripItems(toolStripItems);
            Items.AddRange(toolStripItems.ToArray());

            // Initialize ToolStrip
            Location = new System.Drawing.Point(0, 0);
            Name = "Event Sequence Document Search";
            Size = new System.Drawing.Size(292, 25);
            TabIndex = 0;
            Text = "Event Sequence Document Search";
			GripStyle = ToolStripGripStyle.Hidden;

            // Done
            ResumeLayout(false);
        }

        /// <summary>
        /// Event handler called when user has entered search text params into the toolstrip</summary>
        /// <param name="sender">Sender of the event</param>
        /// <param name="e">Event arguments</param>
        private void searchSubStrip_SearchTextEntered(object sender, System.EventArgs e)
        {
			// Using search predicates generated by the user via this class, trigger the bound data set to start query on its data
			DoSearch();
        }

        /// <summary>
        /// Event handler called when user has changed the search options in the toolstrip</summary>
        /// <param name="sender">Sender of the event</param>
        /// <param name="e">Event arguments</param>
        private void searchSubStrip_OptionsChanged(object sender, System.EventArgs e)
        {
            // Add to the tool strip the items newly represented by this QueryOption
            QueryOption changedOption = sender as QueryOption;
            if (changedOption != null)
            {
                this.Items.Clear();
                List<ToolStripItem> itemList = new List<ToolStripItem>();
                m_rootNode.GetToolStripItems(itemList);
                this.Items.AddRange(itemList.ToArray());
				Sce.Atf.Event.Raise(UIChanged, sender, System.EventArgs.Empty);
			}
        }

        /// <summary>
        /// Add instances of classes that implement IQueryPredicate to define what is searched
        /// </summary>
        /// <returns>Search predicates</returns>
        public override IQueryPredicate GetPredicate()
        {
            // Parse query tree to build list of predicates, with which the search will be made
            return m_rootNode.GetPredicate();
        }

        /// <summary>
        /// Event that is raised after UI has changed</summary>
		public override event EventHandler UIChanged;

		private DomNodeQueryRoot m_rootNode;
    }
}

